package com.alex.server.service.impl;

import com.alex.server.mapper.EmployeeMapper;
import com.alex.server.mapper.MailLogMapper;
import com.alex.server.pojo.*;
import com.alex.server.service.IEmployeeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 员工表 服务实现类
 * </p>
 *
 * @author alex
 * @since 2022-08-30
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Resource
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private MailLogMapper mailLogMapper;
    /**
     * 获取所有员工(分页)
     * @param currentPage
     * @param size
     * @param employee
     * @param beginDataSource
     * @return
     */
    @Override
    public RespPageBean getEmployeeByPage(Integer currentPage, Integer size, Employee employee, LocalDate[] beginDataSource) {
        Page<Employee> page =new Page<>(currentPage,size);
        IPage<Employee> employeeByPage = employeeMapper.getEmployeeByPage(page, employee, beginDataSource);
        RespPageBean respPageBean=new RespPageBean(employeeByPage.getTotal(),employeeByPage.getRecords());
        return respPageBean;
    }

    /**
     * 获取工号
     * @return
     */
    @Override
    public RespBean getMaxWorkId() {
        List<Map<String, Object>> maps = employeeMapper.selectMaps(new QueryWrapper<Employee>().select("max(workId)"));
        Map<String, Object> map = maps.get(0);
        //获得最大工号+1
        Integer workId =  Integer.parseInt(map.get("max(workId)").toString())+1;
        String workID = String.format("%08d", workId);
        return RespBean.success(null,workID);
    }


    /**
     * 添加员工
     * 重点是处理合同期限
     */
    @Override
    public RespBean addEmp(Employee employee) {

        //处理合同期限,保留两位小数
        LocalDate beginContract = employee.getBeginContract();
        LocalDate endContract = employee.getEndContract();
        long days = beginContract.until(endContract, ChronoUnit.DAYS);
        DecimalFormat decimalFormat=new DecimalFormat("##.00");
        employee.setContractTerm(Double.parseDouble(decimalFormat.format(days/365.00)));
        if (1==employeeMapper.insert(employee)) {
            Employee emp = employeeMapper.getEmployee(employee.getId()).get(0);
            //数据库记录发送的消息
            //String msgId = UUID.randomUUID().toString();
            String msgId ="123456";
            MailLog mailLog=new MailLog();
            mailLog.setMsgId(msgId);
            mailLog.setEid(employee.getId());
            mailLog.setStatus(MailConstants.DELIVERING);
            mailLog.setCount(MailConstants.MAX_TRY_COUNT);
            mailLog.setExchange(MailConstants.EXCHANGE_name);
            mailLog.setRouteKey(MailConstants.ROUTING_KEY);
            mailLog.setCreateTime(LocalDateTime.now());
            mailLog.setUpdateTime(LocalDateTime.now());
            mailLog.setTryTime(LocalDateTime.now().plusMinutes(MailConstants.MSG_TIMEOUT));
            mailLogMapper.insert(mailLog);
            //发送消息
            rabbitTemplate.convertAndSend(MailConstants.EXCHANGE_name,MailConstants.ROUTING_KEY,emp,
                      new CorrelationData(msgId));

            return RespBean.success("添加成功");
        }else {
            return RespBean.error("添加失败");
        }
    }

    /**
     * 更新员工
     * @param employee
     * @return
     */
    @Override
    public RespBean upEmp(Employee employee) {
        LocalDate beginContract = employee.getBeginContract();
        LocalDate endContract = employee.getEndContract();
        long days = beginContract.until(endContract, ChronoUnit.DAYS);
        DecimalFormat decimalFormat=new DecimalFormat("##.00");
        employee.setContractTerm(Double.parseDouble(decimalFormat.format(days/365.00)));
        if (1==employeeMapper.updateById(employee)) {
            return RespBean.success("更新成功");
        }else {
            return RespBean.error("更新失败");
        }
    }

    /**
     * 根据id查询员工
     * @param id
     * @return
     */
    @Override
    public List<Employee> getEmployee(Integer id) {

        return employeeMapper.getEmployee(id);
    }

    @Override
    public RespPageBean getAllSobcfg(Integer currentPage, Integer size) {
        Page<Employee> page =new Page<Employee>(currentPage,size);
        IPage<Employee> employeeByPage = employeeMapper.getAllSobcfg(page);
        RespPageBean respPageBean=new RespPageBean(employeeByPage.getTotal(),employeeByPage.getRecords());
        return respPageBean;
    }
}
